//
//  ChangePassWordViewController.m
//  NewPro
//
//  Created by RYX on 2018/10/15.
//  Copyright © 2018年 何荧红. All rights reserved.
//

#import "ChangePassWordViewController.h"
#import "SetStore.h"
#import "LoginViewController.h"

@interface ChangePassWordViewController ()
{
    __weak IBOutlet UITextField *oldPasTf;
    __weak IBOutlet UITextField *newPasTf;
    __weak IBOutlet UITextField *reNewPasTf;
}
@end

@implementation ChangePassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setNavBarWithTitle:@"修改密码"];
}

- (IBAction)onSureBtn:(UIButton *)sender {
    
    if (oldPasTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入旧密码"];
    }
    
    if (newPasTf.text.length <= 0) {
        
        return [self showMBPError:@"请输入新密码"];
    }
    
    if (reNewPasTf.text.length <= 0) {
        
        return [self showMBPError:@"请再次输入新密码"];
    }
    
    if (![newPasTf.text isEqualToString:reNewPasTf.text]) {
        
        return [self showMBPError:@"两次密码不一致"];
    }
    
    SetStore *store = [[SetStore alloc] init];
    
    MJWeakSelf
    [store changePassWordWithCustomId:[UserDefaultsManager sharedInstance].user_id andOldpassword:[oldPasTf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] andNewpassword:[newPasTf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] andRePassWord:[reNewPasTf.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] Success:^{
        
        [weakSelf showMBPError:@"修改成功"];
        
        //GCD延迟
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"id"];
            
            LoginViewController *VC = [[LoginViewController alloc] init];
            
            [weakSelf.navigationController pushViewController:VC animated:YES];
            
        });
        
    } Failure:^(NSError *error) {
       
        [weakSelf showMBPError:[HttpTool handleError:error]];
    }];
}


@end
